package com.leet.code.lcof;

import com.algorithm.model.TreeNode;

import java.util.LinkedList;

public class Code32 {

    public static void main(String[] args) {

    }


    LinkedList<Integer> list = new LinkedList<>();

    public int[] levelOrder(TreeNode root) {

        list.add(root.val);
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        if (root.left != null) {
            treeNodes.addFirst(root.left);
        }

        if (root.right != null) {
            treeNodes.addLast(root.right);
        }
        l(treeNodes);
        int[] rs = new int[list.size()];


        int i = 0;
        for (Integer integer : list) {
            rs[i] = integer;
            i++;
        }
        return rs;
    }


    public void l(LinkedList<TreeNode> treeNodes) {
        if (treeNodes == null || treeNodes.size() == 0) {
            return;
        }

        LinkedList<TreeNode> ts = new LinkedList<>();
        for (TreeNode t : treeNodes) {
            list.addLast(t.val);
            if (t.left != null) {
                ts.addLast(t.left);
            }
            if (t.right != null) {
                ts.addLast(t.right);
            }
        }
        l(ts);
    }

}
